package com.rzico.sbl.ui.manual

import androidx.core.view.isVisible
import com.jeremyliao.liveeventbus.utils.observe
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.RecordData
import com.rzico.sbl.viewmodel.ManualViewModel
import com.xinnuo.common.decoration.DividerLRDecoration
import com.xinnuo.common.extend.condition
import com.xinnuo.common.extend.conditionFalse
import com.xinnuo.common.extend.conditionIf
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.gone
import com.xinnuo.common.extend.notEmpty
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.extend.visibleValue
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.showBottomPhoneDialog
import com.xinnuo.slimadapter.SlimAdapter

class ManualCallActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<ManualViewModel>()

    override fun initData() {
        initTitle(title = "接单列表")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关接单信息！"
                }

                recycleList.also { view ->
                    view.setBackgroundResource(R.color.white)

                    view.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            isLoadingMore
                                .conditionFalse {
                                    isLoadingMore = true
                                    getData(pageNum)
                                }
                        }
                    )

                    view.addItemDecoration(DividerLRDecoration())

                    mAdapter = SlimAdapter.creator()
                        .register<RecordData>(R.layout.item_manual_call_large) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_manual_tel, bean.mobile)
                                text(
                                    R.id.item_manual_time,
                                    bean.createDate.getTimeString()
                                )
                                text(
                                    R.id.item_manual_next,
                                    conditionIf(
                                        bean.status == "confirmed",
                                        "再来一单",
                                        "去下单"
                                    )
                                )

                                visibility(
                                    R.id.item_manual_new,
                                    visibleValue { bean.newer }
                                )

                                visibility(
                                    R.id.item_manual_from,
                                    visibleValue { bean.source == "box" }
                                )

                                clicked(R.id.item_manual_tel) {
                                    showBottomPhoneDialog(bean.mobile)
                                }

                                clicked(R.id.item_manual_next) {
                                    startActivityEx<ManualMobileActivity>(
                                        "mobile" to bean.mobile,
                                        "memberId" to bean.memberId.orEmpty()
                                    )
                                }
                            }
                        }
                        .attachTo(view)
                }
            }
    }

    override fun initListener() {
        observe<String>("phone_report") { updateList() }
    }

    /**
     * 会员收货地址列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .callList(
                page = index,
                onSuccess = {
                    mList.apply {
                        (index == 0)
                            .condition {
                                clear()
                                pageNum = 0
                            }

                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}